package com.cgg.java8.ch01;

import com.cgg.java8.entity.Apple;
import com.cgg.java8.utils.Utils;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author Xiecheng create on 2021/9/14
 *
 * 使用lambda代替匿名内部类，将代码行为作为参数传入
 */
public class Demo03 {
    @Test
    public void testCompare() {
        List<Apple> apples = Arrays.asList(
                new Apple("green", 70),
                new Apple("red", 290),
                new Apple("pink", 160),
                new Apple("yellow", 250)
        );

        // 将apple排序输出
        apples.sort(Comparator.comparingInt(Apple::getWeight));
        Utils.printListAsJson(apples);
    }

    @Test
    public void testThread() {
        new Thread(() -> System.out.println("这是异步线程: " + Thread.currentThread().getName())).start();
        System.out.println("这是主线程: " + Thread.currentThread().getName());
    }
}
